package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.GoodsCombineBo;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsAttribute;
import com.cskaoyan.market.db.domain.MarketGoodsProduct;
import com.cskaoyan.market.db.domain.MarketGoodsSpecification;
import com.cskaoyan.market.service.AdminGoodsService;
import com.cskaoyan.market.service.AdminGoodsServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.GoodsDetailMarketBrandVo;
import com.cskaoyan.market.bo.GoodsUpdateJsonObjVo;
import com.cskaoyan.market.vo.MarketGoodsDetailCategoryVo;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/goods/*")
public class AdminGoodsController extends HttpServlet {
    AdminGoodsService adminGoodsService=new AdminGoodsServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/goods/","");
        if ("list".equals(replace)){
            list(req,resp);
            return;
        }
        if ("detail".equals(replace)){
            detail(req,resp);
            return;
        }
        if ("catAndBrand".equals(replace)){
            catAndBrand(req,resp);

        }

    }

    private void updateGoods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String s = req.getReader().readLine();
        GoodsUpdateJsonObjVo goodsUpdateJsonObjVo=JacksonUtil.getObjectMapper().readValue(s, GoodsUpdateJsonObjVo.class);
        MarketGoods goods = goodsUpdateJsonObjVo.getGoods();

        List<MarketGoodsAttribute> attributes = goodsUpdateJsonObjVo.getAttributes();

        List<MarketGoodsProduct> products = goodsUpdateJsonObjVo.getProducts();

        List<MarketGoodsSpecification> specifications = goodsUpdateJsonObjVo.getSpecifications();

        if(adminGoodsService.updateGoods(goods,attributes,products,specifications)){
            Object ok = ResponseUtil.ok();
            String s1 = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().println(s1);
            return;
        }
        Object fail = ResponseUtil.fail();
        String f = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().println(f);

    }


    private void catAndBrand(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<MarketGoodsDetailCategoryVo>  marketGoodsDetailCategoryVos = adminGoodsService.categoryList();

        Map<String,Object> data=new HashMap<>();
        data.put("categoryList",marketGoodsDetailCategoryVos);

        List<GoodsDetailMarketBrandVo> marketBrandList=adminGoodsService.brandList();
        data.put("brandList",marketBrandList);

        Object o = ResponseUtil.ok(data);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String goodsId = req.getParameter("id");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        Map<String,Object> data=adminGoodsService.detail(goodsId,sort,order);
        Object ok = ResponseUtil.ok(data);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String goodsSn = req.getParameter("goodsSn");
        String name = req.getParameter("name");
        String goodsId = req.getParameter("goodsId");
        List<MarketGoods> marketGoodsList=adminGoodsService.list(page,limit,sort,order,goodsSn,name,goodsId);

        Object o = ResponseUtil.okList(marketGoodsList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/goods/","");
        if ("update".equals(replace)){
            updateGoods(req,resp);
            return;
        }
        if ("delete".equals(replace)){
            delete(req,resp);
        }
        if("creat".equals(replace)){
            create(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String s = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketGoods marketGoods = objectMapper.readValue(s, MarketGoods.class);
        if (adminGoodsService.delete(marketGoods.getId())!=0){
            Object ok = ResponseUtil.ok();
            String s1 = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().println(s1);
            return;
        }
        Object fail = ResponseUtil.fail();
        String f = JacksonUtil.writeValueAsString(fail);
        resp.getWriter().println(f);
    }
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //接收请求参数  json字符串 位于请求体里面的
        BufferedReader reader = req.getReader();
        String requestBody = reader.readLine();
        GoodsCombineBo goodsCombineBo = JacksonUtil.getObjectMapper().readValue(requestBody, GoodsCombineBo.class);
        adminGoodsService.insert(goodsCombineBo.getGoods(), goodsCombineBo.getSpecifications(), goodsCombineBo.getAttributes(), goodsCombineBo.getProducts());

        //抓取公网上面的返回结果
        Object ok = ResponseUtil.ok();
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);
    }
}
